﻿//-----------------------------------------------------------------------
// <copyright file="DataBagValue.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.DataBag
{
    using System;
    using TileTool.DataBag.Generic;

    /// <summary>
    /// Data bag value implementation
    /// </summary>
    /// <typeparam name="T">Value type</typeparam>
    public class DataBagValue<T> : DataBagValueBase, IDataBagValue<T>
    {
        #region Fields - Private

        /// <summary>
        /// Value of this object.
        /// </summary>
        private T value;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        internal DataBagValue()
        {
            this.ValidateType();
            this.Value = default(T);
        }

        /// <summary>
        /// Construct the object with an initial value.
        /// </summary>
        /// <param name="value">Initial value.</param>
        internal DataBagValue(T value)
        {
            this.ValidateType();
            this.Value = value;
        }

        #endregion

        #region Properties - Public

        /// <summary>
        /// Gets or sets the object's value.
        /// </summary>
        public T Value
        {
            get
            {
                return this.value;
            }

            set
            {
                this.value = value;
                this.TriggerValueChanged();
            }
        }

        /// <summary>
        /// Gets the value of the data bag value as an object.
        /// </summary>
        public override object ObjectValue
        {
            get
            {
                return this.value;
            }
        }

        /// <summary>
        /// Gets the type of value.
        /// </summary>
        public override DataBagValueType Type
        {
            get
            {
                return DataBagValueTypeUtility.GetDataBagValueTypeFromType(typeof(T));
            }
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Clone this data bag value by creating a deep copy.
        /// </summary>
        /// <returns>Returns a copy of the data bag value.</returns>
        public override IDataBagValue Clone()
        {
            return new DataBagValue<T>(this.Value);
        }

        /// <summary>
        /// Convert the value to a string.
        /// </summary>
        /// <returns>Returns the object as a string.</returns>
        public override string ToString()
        {
            switch (this.Type)
            {
                case DataBagValueType.Int:
                case DataBagValueType.UInt:
                case DataBagValueType.Long:
                case DataBagValueType.ULong:
                case DataBagValueType.Float:
                case DataBagValueType.String:
                case DataBagValueType.ColorRGBA:
                case DataBagValueType.Vector2D:
                case DataBagValueType.Matrix2D:
                case DataBagValueType.Size2D:
                    return this.Value.ToString();
            }

            throw new InvalidOperationException("Unable to convert type " + this.Type.ToString() + " to string.");
        }

        #endregion

        #region Methods - Private

        /// <summary>
        /// Validate the we have a supported type.
        /// </summary>
        private void ValidateType()
        {
            if (!DataBagValueTypeUtility.IsValidType(typeof(T)))
            {
                throw new InvalidOperationException("Type " + typeof(T).Name + " is not a valid data bag value type.");
            }
        }

        #endregion
    }
}
